package pl.streamsoft.currencyApp.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Cacheable
@Cache(region="Country",usage=CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries({
	@NamedQuery(name="Country.whereNameIsLike",query="from Country where name = ?"),
	@NamedQuery(name="Country.deleteWhereNameIsLike",query="delete from Country where name = ?"),
	@NamedQuery(name="Country.allCountries",query="from Country"),
	@NamedQuery(name="Country.deleteAllCountries",query="delete from Country")
})
@Table(name="Countries")
public class Country {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	@Column(unique=true, nullable= false)
	private String name;

	@ManyToMany(fetch=FetchType.EAGER)
	//fetch join
	@Fetch(FetchMode.JOIN)
	@Cascade(CascadeType.SAVE_UPDATE)
	@JoinTable(joinColumns=@JoinColumn(name="countryId"), inverseJoinColumns=@JoinColumn(name="currencyId"))
	private List<Currency> currencyList;
	
	public Country(){
		this.currencyList = new ArrayList<Currency>();
	}
	
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public List<Currency> getCurrencyList() {
		return currencyList;
	}

	public long getId() {
		return id;
	}


	public void setId(long id) {
		this.id = id;
	}
	
}
